<template>
    <n-card :bordered="false" class="proCard">
        <BasicTable title="院系列表" titleTooltip="niconiconi" :columns="columns" :request="loadDataTable"
            :row-key="(row) => row.academyID" ref="actionRef" :actionColumn="actionColumn" :scroll-x="1360"
            :row-class-name="rowClassName" @update:checked-row-keys="onCheckedRow" default-expand-all>
            <template #tableTitle>
                <n-button type="primary" @click="addTable">
                    <template #icon>
                        <n-icon>
                            <PlusOutlined />
                        </n-icon>
                    </template>
                    新建
                </n-button>
            </template>
            <template #toolbar>
                <n-button type="primary" @click="reloadTable">刷新数据</n-button>
            </template>
        </BasicTable>
        <n-modal v-model:show="showAcademyModal" :show-icon="false" preset="dialog" :title="academyTitle">
            <n-form :model="academyFormParams" :rules="rules" ref="academyFormRef" label-placement="left" :label-width="80"
                class="py-4">
                <n-form-item label="学院ID" path="academyID" style="display:none">
                    <n-input placeholder="请输入名称" v-model:value="academyFormParams.academyID" />
                </n-form-item>
                <n-form-item label="学院名" path="academyName">
                    <n-input v-model:value="academyFormParams.academyName" />
                </n-form-item>
            </n-form>
            <template #action>
                <n-space>
                    <n-button @click="() => (showAcademyModal = false)">取消</n-button>
                    <n-button type="info" :loading="formBtnLoading" @click="confirmAcademyForm">确定</n-button>
                </n-space>
            </template>
        </n-modal>
        <n-modal v-model:show="showMajorModal" :show-icon="false" preset="dialog" :title="academyTitle">
            <n-form :model="majorFormParams" :rules="majorRules" ref="majorFormRef" label-placement="left" :label-width="80"
                class="py-4">
                <n-form-item label="学院ID" path="academyID" style="display:none">
                    <n-input v-model:value="majorFormParams.academyID" />
                </n-form-item>
                <n-form-item label="学院名" path="academyName">
                    <n-input readonly v-model:value="majorFormParams.academyName" />
                </n-form-item>
                <n-form-item label="专业ID" path="academyID" style="display:none">
                    <n-input v-model:value="majorFormParams.majorID" />
                </n-form-item>
                <n-form-item label="专业名" path="majorName">
                    <n-input placeholder="请输入名称" v-model:value="majorFormParams.majorName" />
                </n-form-item>
            </n-form>
            <template #action>
                <n-space>
                    <n-button @click="() => (showMajorModal = false)">取消</n-button>
                    <n-button type="info" :loading="formBtnLoading" @click="confirmMajorForm">确定</n-button>
                </n-space>
            </template>
        </n-modal>
    </n-card>
</template>
<script lang="ts" setup>
import { reactive, ref, h, } from 'vue';
import { BasicTable, TableAction } from '@/components/Table';
import {columns} from './academyMajorColumns';
import { getTableList } from '@/api/manager/academyMajorManage/list';
import { useDialog, useMessage } from 'naive-ui';
import { PlusOutlined } from '@vicons/antd';
import { http } from '@/utils/http/axios';

const rules = []
const majorRules = []
const showAcademyModal = ref(false);
const showMajorModal = ref(false);
const academyTitle = ref("");
const academyFormParams = reactive({
    academyID: null,
    academyName: null,
});
const majorFormParams = reactive(
    {
        majorID: null,
        majorName: null,
        academyID: null,
        academyName: null
    }
)
const formBtnLoading = ref(false);

const message = useMessage();
const dialog = useDialog();

const academyFormRef = ref();
const majorFormRef = ref();

function confirmAcademyForm(e) {
    e.preventDefault();
        formBtnLoading.value = true;
        academyFormRef.value.validate((errors) => {
            if (!errors) {
            //message.success('新建成功');
            let params = academyFormParams;
            let url = '';
            if(academyTitle.value == "新建"){
                url = '/manager/academy_major_manage/addAcademy' 
            }else{
                url = '/manager/academy_major_manage/editAcademy'
            }
            http.request({
                url: url,
                method: 'post',
                params
            }).then(
                () => {
                message.success('成功');
                showAcademyModal.value = false;
                reloadTable();
                }
            );
            } else {
            message.error('请填写完整信息');
            }
            formBtnLoading.value = false;
        });
}

function confirmMajorForm(e) {
        e.preventDefault();
        formBtnLoading.value = true;
        majorFormRef.value.validate((errors) => {
            if (!errors) {
            //message.success('新建成功');
            let params = majorFormParams;
            let url = '';
            if(academyTitle.value == "新建"){
                url = '/manager/academy_major_manage/addMajor' 
            }else{
                url = '/manager/academy_major_manage/editMajor'
            }
            http.request({
                url: url,
                method: 'post',
                params
            }).then(
                () => {
                message.success('成功');
                showMajorModal.value = false;
                reloadTable();
                }
            );
            } else {
            message.error('请填写完整信息');
            }
            formBtnLoading.value = false;
        });
}

const params = reactive({
    pageSize: 10
});

const loadDataTable = async (res) => {
    let result = await getTableList({ ...params, ...res });
    let rt = {
        list: result
    };
    rt.list.children = [];
    console.log(rt);
    rt.list.forEach(element => {
        element['children'] = [];
        element['majorList'].forEach(el => {
            element['children'].push(
                {
                    academyID: +el['majorID'],
                    academyName: el['majorName'],
                    rawacademyName: element['academyName'],
                    majorID: el['majorID']
                }
            )
        });
    });
    return rt
};

const actionColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
        return h(TableAction, {
            style: 'button',
            actions: createActions(record),
        });
    },
});

function handleDelete(record) {

    dialog.info({
        title: '提示',
        content: `您想删除${record.academyName}`,
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick: () => {
            //message.success('删除成功');
            let getparam = {};
            let url="";
            if (record['isLeaf'] != null) {
                getparam['academyID'] = record['academyID'];
                url='/manager/academy_major_manage/deleteAcademy';
            } else {
                getparam['majorID'] = record['majorID'];
                url='/manager/academy_major_manage/deleteMajor';
            }
            http.request({
                url: url,
                method: 'get',
                params: getparam
            }
            ).then(
                () => {
                    message.success('成功');
                    reloadTable();
                }
            );
        },
        onNegativeClick: () => { },
    });
}

function handleEdit(record) {
    if (record.isLeaf != null) {
        academyTitle.value = '修改';
        academyFormParams['academyID'] = record['academyID'];
        academyFormParams['academyName'] = record['academyName'];
        showAcademyModal.value = true;
    } else {
        academyTitle.value = '修改';
        majorFormParams['majorID'] = record['majorID'];
        majorFormParams['majorName'] = record['academyName'];
        majorFormParams['academyName'] = record['rawacademyName'];
        majorFormParams['academyID'] = record['academyID'];
        showMajorModal.value = true;
    }
}

function rowClassName(row) {
    console.log(row);
    if (row.majorList != null) {
        return '';
    } else {
        return 'leaf-row'
    }
}

function createActions(record) {
    return [
        {
            label: '添加专业',
            onClick: addMajorTable.bind(null, record),
            ifShow: () => {
                console.log(record);
                if (record.isLeaf != null) {
                    return true;
                } else {
                    return false;
                }
            },
            auth: ['basic_list'],
        },
        {
            label: '修改',
            onClick: handleEdit.bind(null, record),
            ifShow: () => {
                return true;
            },
            auth: ['basic_list'],
        }, {
            label: '删除',
            icon: 'ic:outline-delete-outline',
            onClick: handleDelete.bind(null, record),
            // 根据业务控制是否显示 isShow 和 auth 是并且关系
            ifShow: () => {
                return true;
            },
            // 根据权限控制是否显示: 有权限，会显示，支持多个
            auth: ['basic_list'],
        },

    ];
}
const actionRef = ref();
function onCheckedRow() {

}
function addTable() {
    academyTitle.value = '新建';
    academyFormParams['academyID'] = null;
    academyFormParams['academyName'] = null;
    showAcademyModal.value = true;
}

function addMajorTable(record) {
    academyTitle.value = '新建';
    majorFormParams['majorID'] = null;
    majorFormParams['majorName'] = null;
    majorFormParams['academyName'] = record['academyName'];
    majorFormParams['academyID'] = record['academyID'];
    showMajorModal.value = true;
}

function reloadTable() {
    actionRef.value.reload();
}
</script>
<style lang="less" scoped>
:deep(.leaf-row td) {
    background-color: #ecf0f1 !important;
}
</style>

